package volkov.ja.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementDecorator;
import volkov.ja.util.PageUtils;
import volkov.ja.util.TestLogger;

public class PageManager {

	private WebDriver driver;
	private WebDriverWait wait;
	private PageUtils utils;
	private TestLogger log;

	// TODO: encapsulate, enumeration
	public LoginPage loginPage;
	public InternalPage internalPage;
	public UserProfilePage userProfilePage;
	public UserManagementPage userManagementPage;
	public MovieListPage movieListPage;
	public MovieCreatePage movieCreatePage;
	public MovieViewPage movieViewPage;
	public MovieEditPage movieEditPage;

	public PageManager(WebDriver driver, TestLogger log) {
		this.driver = driver;
		wait = new WebDriverWait(driver, 10);
		utils = new PageUtils(driver);
		this.log = log;

		loginPage = initElements(new LoginPage(this));
		internalPage = initElements(new InternalPage(this));
		userProfilePage = initElements(new UserProfilePage(this));
		userManagementPage = initElements(new UserManagementPage(this));
		movieListPage = initElements(new MovieListPage(this));
		movieCreatePage = initElements(new MovieCreatePage(this));
		movieViewPage = initElements(new MovieViewPage(this));
		movieEditPage = initElements(new MovieEditPage(this));
	}

	private <T extends Page> T initElements(T page) {
		// PageFactory.initElements(driver, page);
		// PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10),
		// page);
		// the preferred way without HtmlElements
		// PageFactory.initElements(
		// new DisplayedElementLocatorFactory(driver, 10), page);

		HtmlElementDecorator hed = new HtmlElementDecorator(
				new DisplayedElementLocatorFactory(driver, 10));
		PageFactory.initElements(hed, page);

		// PageFactory.initElements(new HtmlElementDecorator(driver), page);

		return page;
	}

	public WebDriver getWebDriver() {
		return driver;
	}

	public WebDriverWait getWait() {
		return wait;
	}

	public PageUtils getPageUtils() {
		return utils;
	}

	public TestLogger getLog() {
		return log;
	}

}
